import React from 'react';
import { Card, Form, message, Table } from 'antd';
import SearchForm from '../../components/AutoForm/SearchForm';
import { connect } from 'dva';
import moment from 'moment';
import 'moment/locale/zh-cn';
import { stringify } from 'qs';
import { baseServerUrl } from '../../utils/Constant';
import { getAuthority } from '../../utils/authority';
moment.locale('zh-cn');

@connect(({ orderReport, loading }) => ({
  tableLoading: loading.effects['orderReport/page'],
  tableData: orderReport.pageList,
  pagination: orderReport.pagination,
  columns: orderReport.columns,
  searchFields: orderReport.searchFields,
  searchValues: orderReport.searchValues,
  selectionIds: orderReport.selectionIds,
  selectedRows: orderReport.selectedRows,
}))
class EditableTable extends React.Component {
  rowClassName = (record, index) => {
    if (record.feeSum > record.feeInSum) {
      return 'orderWarning';
    }
  };

  componentDidMount() {
    this.handleSearch();
  }
  handleSearchChange = fields => {
    const { dispatch } = this.props;
    dispatch({
      type: 'orderReport/handleSearchChange',
      payload: fields,
    });
  };

  handleSearch = (flag) => {
    const { dispatch, pagination, searchValues } = this.props;
    let pageParam = { pageSize: pagination.pageSize, current: flag==='btn' ? 1 : pagination.current };
    let searchParam = {};
    Object.keys(searchValues).map(key => {
      searchParam[key] = searchValues[key]['value'];
      if (key === 'createTimeArray') {
        const dateArray = searchValues[key]['value'];
        if (dateArray && dateArray.length) {
          let startTime = moment(dateArray[0]);
          let endTime = moment(dateArray[1]);
          /* searchParam[key] = [];
          searchParam[key].push(startTime.add(8, 'h'));
          searchParam[key].push(endTime.add(8, 'h'));*/
          searchParam[key] = [];
          searchParam[key].push(startTime.format('YYYY-MM-DD HH:mm:ss'));
          searchParam[key].push(endTime.format('YYYY-MM-DD HH:mm:ss'));
        }
      }
    });
    const param = {
      ...searchParam,
      ...pageParam,
      flag: 'report',
    };
    dispatch({
      type: 'orderReport/page',
      payload: param,
    });
  };

  handleDownload = () => {
    const { searchValues } = this.props;
    let searchParam = {};
    Object.keys(searchValues).map(key => {
      searchParam[key] = searchValues[key]['value'];
      if (key === 'createTimeArray') {
        const dateArray = searchValues[key]['value'];
        if (dateArray.length) {
          let startTime = moment(dateArray[0]);
          let endTime = moment(dateArray[1]);
          searchParam[key] = [];
          searchParam[key].push(startTime.format('YYYY-MM-DD HH:mm:ss'));
          searchParam[key].push(endTime.format('YYYY-MM-DD HH:mm:ss'));
        }
      }
    });
    searchParam.token = getAuthority();
    window.open(`${baseServerUrl}/tms/report/orderExport?${stringify(searchParam)}`);
  };

  onChange = (selectedRowKeys, selectedRows) => {
    const { dispatch } = this.props;
    dispatch({
      type: 'orderReport/handleSelection',
      payload: { selectedRowKeys, selectedRows },
    });
  };

  render() {
    const {
      columns,
      pagination,
      tableLoading,
      tableData,
      searchFields,
      searchValues,
      dispatch,
    } = this.props;

    pagination.onChange = (page, pageSize) => {
      let searchParam = {};
      Object.keys(searchValues).map(key => {
        searchParam[key] = searchValues[key]['value'];
      });
      dispatch({
        type: 'orderReport/page',
        payload: {
          ...searchParam,
          current: page,
          flag: 'report',
          pageSize,
        },
      });
    };
    pagination.onShowSizeChange = (current, size) => {
      let searchParam = {};
      Object.keys(searchValues).map(key => {
        searchParam[key] = searchValues[key]['value'];
      });
      dispatch({
        type: 'orderReport/page',
        payload: {
          ...searchParam,
          current: current,
          flag: 'report',
          pageSize: size,
        },
      });
    };

    /*const rowSelection = {
      type: 'checkbox',
      selections: this.props.selectedRows,
      selectedRowKeys: this.props.selectionIds,
      onChange: this.onChange,
      getCheckboxProps: record => ({
        disabled: record.status !== 'GATHERING_AUDIT', // Column configuration not to be checked
      }),
    };*/

    return (
      <Card bodyStyle={{ padding: 0 }}>
        <SearchForm
          header="订单日报->操作"
          layout="inline"
          isDownload={true}
          onDownload={this.handleDownload}
          onFieldsChange={this.handleSearchChange}
          onSubmit={() => {this.handleSearch('btn')}}
          fields={searchFields}
          fieldValue={searchValues}
        />

        <Table
          bordered
          rowKey="id"
          loading={tableLoading}
          dataSource={tableData}
          columns={columns}
          rowClassName={this.rowClassName}
          scroll={{ x: 'max-content' }}
          size="small"
          pagination={pagination}
        />
      </Card>
    );
  }
}

const EditableFormTable = Form.create()(EditableTable);
export default EditableFormTable;
